package com.zzh.base.utils;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.support.annotation.IdRes;
import android.support.annotation.RequiresApi;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

public class AutoUtils {

    private static int sScreenWidth;
    private static int sScreenHeight;

    private static float sAutoScaleX;
    private static float sAutoScaleY;

    private static final boolean DEFAULT_ENABLE = true;

    private static double sTextAutoScale;

    public static void setSize(Activity act, boolean hasStatusBar, int UIWidth, int UIHeight){
        if(act == null || UIWidth < 1 || UIHeight < 1)return;

        DisplayMetrics screenSize = WindowUtils.getScreenSize(act);
        int width = screenSize.widthPixels;
        int height = screenSize.heightPixels;

        if (hasStatusBar) {
            height -= WindowUtils.getStatusHeight(act);
        }

        AutoUtils.sScreenWidth = width;
        AutoUtils.sScreenHeight = height;

        sAutoScaleX = sScreenWidth * 1.0f / UIWidth;
        sAutoScaleY = sScreenHeight * 1.0f / UIHeight;

        double displayDiagonal = Math.sqrt(Math.pow(AutoUtils.sScreenWidth, 2) + Math.pow(AutoUtils.sScreenHeight, 2));
        double designDiagonal = Math.sqrt(Math.pow(UIWidth, 2) + Math.pow(UIHeight, 2));
        AutoUtils.sTextAutoScale = displayDiagonal / designDiagonal;
    }
    public static void auto(Activity act){
        if(act == null || sScreenWidth < 1 || sScreenHeight < 1)return;
        View view = act.getWindow().getDecorView();
        auto(view, DEFAULT_ENABLE);
    }

    public static void auto(Dialog dialog){
        if(dialog == null || sScreenWidth < 1 || sScreenHeight < 1)return;
        View view = dialog.getWindow().getDecorView();
        auto(view, DEFAULT_ENABLE);
    }

    public static void auto(PopupWindow popupWindow){
        if(popupWindow == null || sScreenWidth < 1 || sScreenHeight < 1)return;
        View view = popupWindow.getContentView();
        auto(view, DEFAULT_ENABLE);
    }


    public static void auto(View view){
        auto(view, DEFAULT_ENABLE);
    }

    public static void auto(View view, boolean autoScale){
        auto(view, autoScale, null);
    }

    public static void auto(View view, OnSizeChangeListener listener){
        auto(view, DEFAULT_ENABLE, listener);
    }


    public static void auto(View view, boolean autoScale, OnSizeChangeListener listener){
        if(view == null || sScreenWidth < 1 || sScreenHeight < 1)return;
        if (sAutoScaleX == 1 && sAutoScaleY == 1) return;

        if (listener != null && !listener.onPrepareChange(view, view.getId())){
            return;
        }

        autoTextSize(view);
        autoSize(view, autoScale);
        autoPadding(view);
        autoMargin(view);
//		autoShape(view, minScale);

        if(view instanceof ViewGroup){
            auto((ViewGroup)view, autoScale, listener);
        }
    }



    public interface OnSizeChangeListener{
        boolean onPrepareChange(View view, @IdRes int id);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private static void autoShape(View view, boolean autoScale) {

        if (view.getBackground() instanceof GradientDrawable) {
            GradientDrawable background = (GradientDrawable) view.getBackground();
            float cornerRadius = background.getCornerRadius();
            try{
                float[] cornerRadii = background.getCornerRadii();
                for (int i = 0; i < cornerRadii.length; i++) cornerRadii[i] = i % 2 == 0 ? getScaleWidth((int) cornerRadii[i], autoScale) : getScaleHeight((int) cornerRadii[i], autoScale);
                background.setCornerRadii(cornerRadii);
            }catch (Exception e){

            }
            background.setCornerRadius((getScaleWidth((int) cornerRadius, autoScale) + getScaleHeight((int) cornerRadius, autoScale)) / 2);
            view.setBackground(background);
        }
    }

    private static void auto(ViewGroup viewGroup, boolean autoScale, OnSizeChangeListener listener){
        int count = viewGroup.getChildCount();

        for (int i = 0; i < count; i++) {

            View child = viewGroup.getChildAt(i);

            if(child != null){
                auto(child, autoScale, listener);
            }
        }
    }

    public static void autoMargin(View view){
        if (!(view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams))
            return;

        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        if(lp == null) return;

        lp.leftMargin = getScaleWidth(lp.leftMargin);
        lp.topMargin = getScaleHeight(lp.topMargin);
        lp.rightMargin = getScaleWidth(lp.rightMargin);
        lp.bottomMargin = getScaleHeight(lp.bottomMargin);

    }

    public static void autoPadding(View view){
        int l = view.getPaddingLeft();
        int t = view.getPaddingTop();
        int r = view.getPaddingRight();
        int b = view.getPaddingBottom();

        l = getScaleWidth(l);
        t = getScaleHeight(t);
        r = getScaleWidth(r);
        b = getScaleHeight(b);

        view.setPadding(l, t, r, b);
    }

    public static void autoSize(View view){
        autoSize(view, DEFAULT_ENABLE);
    }

    public static void autoSize(View view, boolean autoScale){
        ViewGroup.LayoutParams lp = view.getLayoutParams();

        if (lp == null) return;

        if(lp.width > 0){
            lp.width = getScaleWidth(lp.width, lp.width == lp.height);
        }

        if(lp.height > 0){
            lp.height = getScaleHeight(lp.height, lp.width == lp.height);
        }

        int minWidth = view.getMinimumWidth();
        int minHeight = view.getMinimumHeight();

        if (minWidth != 0)view.setMinimumWidth(getScaleWidth(minWidth, autoScale));
        if (minHeight != 0)view.setMinimumHeight(getScaleHeight(minHeight, autoScale));

    }

    public static void autoTextSize(View view){
        if(view instanceof TextView){
            double designPixels = ((TextView) view).getTextSize();
            double displayPixels = sTextAutoScale * designPixels;
            ((TextView) view).setIncludeFontPadding(false);
            ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) displayPixels);
        }
    }

    public static int getScaleWidth(int width){
        return getScaleWidth(width, DEFAULT_ENABLE);
    }

    public static int getScaleWidth(int width, boolean autoScale){
        if(width < 2) return width;
        return (int) (width * (autoScale ? sAutoScaleX < sAutoScaleY ? sAutoScaleX : sAutoScaleY : sAutoScaleX));
    }

    public static int getScaleHeight(int height){
        return getScaleHeight(height, DEFAULT_ENABLE);
    }

    public static int getScaleHeight(int height, boolean autoScale){
        if(height < 2) return height;
        return (int) (height * (autoScale ? sAutoScaleX < sAutoScaleY ? sAutoScaleX : sAutoScaleY : sAutoScaleY));
    }
}
